<!--
 * @Author: 王鹏 2911783749@qq.com
 * @Date: 2022-10-21 14:24:43
 * @LastEditors: 王鹏 2911783749@qq.com
 * @LastEditTime: 2022-10-30 19:18:33
 * @FilePath: \v_shop\code\src\components\children\Users.vue
 * @Description: 二级路由用户列表页
-->
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="16"
          ><div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              class="input-with-select"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="searchUsersList"
              ></el-button>
            </el-input>
            <el-button type="primary" @click="showUsersModule"
              >添加用户</el-button
            >
          </div></el-col
        >
        <el-col :span="8"><div class="grid-content bg-purple"></div></el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="usersList" border style="width: 100%" max-height="400">
        <el-table-column fixed label="#" type="index" align="center">
        </el-table-column>
        <el-table-column prop="username" label="姓名" align="center">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" align="center">
        </el-table-column>
        <el-table-column prop="mobile" label="电话" align="center">
        </el-table-column>
        <el-table-column prop="role_name" label="角色" align="center">
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.mg_state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="changeState(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(scope.$index, scope.row)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delOne(scope.$index, scope.row)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-setting"
              size="mini"
              @click="setRole(scope.$index, scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 弹窗 -->
      <Dialog
        :dialog="dialog"
        :usersForm="usersForm"
        @upDateUsers="getUsersList"
        :rolesList="rolesList"
        :userInfo="userInfo"
      />
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
// 导入api中封装好的接口
import { usersList, updateState, del_user,roles } from "../../http/api";
import Dialog from "../../views/UsersDialog.vue";
export default {
  name: "users",
  data() {
    return {
      // 用户列表数据参数
      queryInfo: {
        query: "", //查询参数
        pagenum: 1, //当前页数
        pagesize: 5, //每页条数
      },
      total: 0, //总条数
      userInfo:{}, //当前角色的信息
      rolesList:[],
      // 用户列表
      usersList: [],
      // 模态框属性
      dialog: {
        // 控制模态框的显隐
        show: false,
        // 模态框标题
        title: "编辑用户",
        // 模态框执行状态
        option: "edit",
      },
      // 模态框表单
      usersForm: {
        username: "",
        password: "",
        email: "",
        mobile: "",
      },
    };
  },

  components: {
    Dialog,
  },

  computed: {},

  mounted() {
    this.getUsersList();
  },

  methods: { 
    // 控制分页中每页条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getUsersList();
    },
    // 控制分页中当前页数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val;
      this.getUsersList();
    },
    // 获取用户列表数据
    getUsersList() {
      usersList(this.queryInfo).then((res) => {
        console.log(res);
        this.usersList = res.data.data.users;
        this.total = res.data.data.total;
      });
    },
    // 更改用户状态
    changeState(state) {
      updateState(state).then((res) => {
        console.log(res);
        this.$message.success(res.data.meta.msg);
        this.getUsersList();
      });
    },
    // 分配角色模态框
    setRole(index,value){
      // 打开分配角色对话框
      this.dialog={
        show:true,
        title:'分配角色',
        option:'set_role'
      }
      this.userInfo=value
      roles().then(res=>{
        console.log(res);
        this.rolesList=res.data.data
      })
    },
    // 添加用户模态框
    showUsersModule() {
      (this.usersForm = {
        username: "",
        email: "",
        mobile: "",
      }),
        (this.dialog = {
          show: true,
          title: "添加用户",
          option: "add",
        });
    },
    // 编辑数据回填
    edit(index, value) {
      this.usersForm = {
        id: value.id,
        username: value.username,
        email: value.email,
        mobile: value.mobile,
      };
      this.dialog = {
        show: true,
        title: "编辑用户",
        option: "edit",
      };
    },
    // 删除单个用户
    delOne(index, value) {
      del_user(value).then((res) => {
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            console.log(res);
            if(res.data.meta.status!==200){
              return this.$message.error(res.data.meta.msg)
            }
            this.$message.success(res.data.meta.msg);
            this.getUsersList();
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      });
    },
    // 搜索
    searchUsersList() {
      usersList(this.queryInfo).then((res) => {
        console.log(res);
        this.$message.success(res.data.meta.msg);
        this.usersList = res.data.data.users;
      });
    },
  },
  watch: {},
  mixins: [],
};
</script>
<style  scoped>
.el-card {
  margin-top: 20px;
}
.input-with-select {
  width: 450px;
  margin-right: 20px;
}
</style>